<template>
  <el-card>
    <div class="box">
      <div class="head">
        <el-button type="primary" class="head_button" @click="openDialog(0, 1)">
          新增<el-icon class="el-icon--right">
            <Upload />
          </el-icon>
        </el-button>
      </div>
      <div class="content">
        <el-table ref="multipleTableRef" :data="items.slice((currentPage - 1) * pageSize, currentPage * pageSize)
          " style="width: 100%; height: 62vh" :header-cell-style="{ 'text-align': 'center', 'font-size': '14px' }"
          :cell-style="{ 'text-align': 'center' }">
          <el-table-column prop="title" label="标题" />
          <el-table-column prop="content" label="内容" show-overflow-tooltip />
          <el-table-column prop="time" label="发布时间" show-overflow-tooltip />
          <el-table-column prop="type" label="类型" show-overflow-tooltip>
            <template #default="scope">
              {{
                scope.row.type == 0
                  ? "通知公告"
                  : scope.row.type == 1
                    ? "政策文件"
                    : "帮助中心"
              }}
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template #default="scope">
              <el-button link type="primary" @click="openDialog(scope.row.id, 0)">编辑</el-button>
              <el-button link type="primary" @click="noticeDel(scope.row.id)">移除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="page">
          <el-pagination v-model:current-page="currentPage" :page-size="pageSize" layout="total, prev, pager, next"
            background :total="items.length" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
      </div>
      <el-dialog v-model="dialogNotice" title="公告信息" width="500" :before-close="false" :show-close="false"
        :close-on-click-modal="false">
        <el-form :model="submitParams" :rules="rules" ref="submitForm">
          <el-form-item label="标题：" prop="title">
            <el-input type="text" v-model="submitParams.title" class="submit" />
          </el-form-item>
          <el-form-item label="内容：" prop="content">
            <el-input type="textarea" v-model="submitParams.content" class="submit" />
          </el-form-item>
          <el-form-item label="类型：" prop="type">
            <el-select v-model="submitParams.type" class="submit">
              <el-option value="0" label="通知公告"></el-option>
              <el-option value="1" label="政策文件"></el-option>
              <el-option value="2" label="帮助中心"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item v-if="timeFlag" label="时间：" prop="time"><el-input type="text" v-model="submitParams.time"
              disabled /></el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="closeDialog()">关闭</el-button>
            <el-button type="primary" @click="manageNotice()"> 保存 </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </el-card>
</template>
<script>
import {
  noticeAll,
  addNotice,
  updateNotice,
  queryNoticeById,
  delNotice,
} from "@/api/notice";
import { pageMixin } from "@/utils/mixin/pageMixin";
import { Upload } from "@element-plus/icons-vue";
import { closeDialog } from "vant";
export default {
  mixins: [pageMixin],
  data() {
    return {
      timeFlag: false,
      dialogNotice: false,
      items: [],
      submitParams: {},
      rules: {
        title: [
          {
            required: true,
            message: "请输入标题",
            trigger: "blur",
          },
        ],
        content: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
        type: [
          {
            required: true,
            message: "请选择类型",
            trigger: "change"
          },
        ],
      },
    };
  },
  created() {
    this.getAll();
  },
  methods: {
    getAll() {
      noticeAll().then((res) => {
        this.items = res.data.data;
      });
    },
    openDialog(id, flag) {
      //0是点击编辑，1是点击新增
      if (flag == 0) {
        this.timeFlag = true;
        queryNoticeById({ id: id }).then((res) => {
          this.submitParams = res.data.data[0];
          this.submitParams.type = this.submitParams.type.toString();
        });
        this.dialogNotice = true;
      } else {
        this.timeFlag = false;
        this.dialogNotice = true;
      }
    },
    manageNotice() {
      this.$refs.submitForm.validate((valid) => {
        if (valid) {
          if (!this.timeFlag) {
            addNotice(this.submitParams).then((res) => {
              this.getAll();
              this.closeDialog();
              this.$message({
                type: "success",
                message: "添加成功!",
              });
              this.submitParams = {};
            });
          } else {
            updateNotice(this.submitParams).then((res) => {
              this.getAll();
              this.closeDialog();
              this.$message({
                type: "success",
                message: "更新成功!",
              });
              this.submitParams = {};
            });
          }
        } else {
          return false;
        }
      });
    },
    closeDialog() {
      this.$refs.submitForm.resetFields()
      this.submitParams = {};
      this.dialogNotice = false;
    },
    noticeDel(id) {
      this.$confirm("此操作将永久删除该公告, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delNotice({ id: id }).then((res) => {
            this.getAll();
          });
        })
        .then(() => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>
<style></style>
<style scoped lang="scss">
.box {
  width: 100%;
  height: 77vh;

  .head {
    width: 100%;
    height: 10vh;
    position: relative;

    .head_button {
      position: absolute;
      right: 0;
      bottom: 0;
      width: 100px;
      height: 40px;
    }
  }

  .content {
    .page {
      display: flex;
      flex-direction: column;
      justify-content: center;
      padding-right: 20px;
      align-items: end;
      width: 100%;
      height: 40px;
      background-color: white;
    }
  }

  .submit {
    width: 100%;
  }
}
</style>
